<template>
  <div>
    <!-- 经销商收车地址维护 -->
    <TopFilter
      :list="topFilterList"
      :cols="3"
      @filterChange="changeHandle"
      @onCollapse="collapseHandle"
      :labelWidth="110"
    ></TopFilter>

    <FilterTable
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjMoveStorehouse"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => this.columns = columns"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button  size="small" type="primary" @click="addHandle(1)" >新 增</multiuse-button>
        <multiuse-button  size="small" type="primary" :click="addHandle.bind(this,2)" :disabled="isSelected">批量确认</multiuse-button>
        <multiuse-button  size="small" type="primary" @click="addHandle(3)" >移库导入</multiuse-button>
      </template>
    </FilterTable>
    <Drawer
      :visible.sync="isEmit"
      :title="'编辑'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <emitModel  :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
    <Drawer
      :visible.sync="isAdd"
      :title="'新增'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <addModel   @close="addcloseHandler" />
    </Drawer>
    <BaseDialog
      :visible.sync="visible"
      :title="'导入'"
      destroyOnClose
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <importFiles  :rowData="formData" @close="importcloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import {getList,postConfirm} from '@/api/dealerSale/InventoryManagement/MoveStorehouse'
import emitModel from './emitModel'
import addModel from './addModel'
import importFiles from './importFiles'
import { dictionary } from '@/utils/dictMixin';
export default {
  name:'TemplateDistribution',
  data(){
    this.BaseTable=null;
    return{
      topFilterList:this.createdFrom(),
      columns:this.createdTable(),
      fetchParams:{},
      fetchApi:getList,
      formData:{},
      isEmit:false,
      isAdd:false,
      visible:false,
      isSelected:true,
      selectedList:[]
    }
  },
  components: {
    emitModel,
    addModel,
    importFiles
  },
  mixins:[dictionary],
  mounted(){
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created(){

  },
  methods:{
    createdFrom(){
      return [
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vin',
          placeholder: '请输入',
          maxlength:'',
          maxRows:2
        },
        {
          type: 'INPUT',
          label: '移库人',
          fieldName: 'transferPersonName',
          placeholder: '请输入',
        },
        {
          type: 'INPUT',
          label: '移库原因',
          fieldName: 'transferReason',
          placeholder: '请输入',
        },
        {
          type: 'RANGE_DATE',
          label: '移库日期',
          fieldName: 'beginTransferDate|endTransferDate',
          placeholder: '请选择',
          initialValue:[],
          dateType:'exactdaterange'
        },
        {
          type: 'INPUT',
          label: '车系名称',
          fieldName: 'seriesName',
          placeholder: '请输入',
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList:this.createDictList('6008')
        },
      ]
    },
    createdTable(){
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>

                <multiuse-button  size="mini" type="text" disabled={props.row.transferStatus==60391001} onClick={() => this.editHandle(props.row,1)}>
                  编辑
                </multiuse-button>

              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'transferStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6039'),
          editItems:this.createDictList('6039'),
        },
        {
          title: '移库单号',
          dataIndex: 'transferNo',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          editItems:this.createDictList('6008'),
        },

        {
          title: '产品名称',
          dataIndex: 'productname',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input',
        },
        {
          title: '移库人',
          dataIndex: 'transferPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原仓库',
          dataIndex: 'fromWarehouseName',
          sorter: true,
          filter: true,
          filterType: 'input',
        },
         {
          title: '新仓库',
          dataIndex: 'toWarehouseName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '移库日期',
          dataIndex: 'transferDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },

        {
          title: '车辆状态',
          dataIndex: 'vehicleStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6040'),
          editItems:this.createDictList('6040'),

        },
        {
          title: '移库原因',
          dataIndex: 'transferReason',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
      ]
    },
    selectChangeHandle(row){
      if(row.length>0){
        this.isSelected=false;
        let is=true;
        row.map(x=>{
          if(x.transferStatus==60391001){
            // 如果有移库的就变为false
            is=false;
          }
        })
        this.selectedList=row;
        // 如果有移库的就disabled
        this.isSelected=is?false:true;
      }else{
        this.isSelected=true;
        this.selectedList=[]
      }

    },
    async addHandle(type){
      if(type==1){
        this.isAdd=true;
      }else if(type==2){
        await this.$confirm('是否确认移库?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        }).then(async () => {
          let arr=[];
          this.selectedList.map(x=>{
            arr.push(x.transferId)
          })
          let res=await postConfirm(arr)
          if (res.resultCode == 200) {
            this.$notify({
              title: '成功',
              message: '确认成功',
              type: 'success'
            });
            this.fetchParams=Object.assign({},this.fetchParams)
          }
        })
        .catch(()=>{})
      }else{
        this.visible=true;
      }
    },
    editHandle(data){
      this.formData=data;
      this.isEmit=true;
    },
    addcloseHandler(val){
      this.isAdd=false;
      if(val){
        this.fetchParams=Object.assign({},this.fetchParams)
      }

    },
    emitcloseHandler(val){
      this.isEmit=false;
      if(val){
        this.fetchParams=Object.assign({},this.fetchParams)
      }
    },
    importcloseHandler(){
      this.visible=false;
      this.fetchParams=Object.assign({},this.fetchParams)
    },
    changeHandle(params){
      this.fetchParams=Object.assign({},this.fetchParams,params)
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },

  }
}
</script>
